// @ts-nocheck
const { Server } = require('ws');

const server = new Server({ port: 8080 });

// state:0,离线，1，在线，2，接听
const users = new Map();

// 处理建立链接事件，给每个链接监听事件
server.on('connection', (link) => {
  link.on('message', (e) => handleMessage(JSON.parse(e.toString()), link));
  link.on('close', () => {
    users.forEach((item, key, map) => {
      if (item.socket === link) {
        map.delete(key)
      }
    })
  });
  link.send(transform({ event: "link", message: "连接服务器成功" }))
});

// 处理每个链接的消息
const handleMessage = (data, link) => {
  const { event, value } = data
  if (event === 'login') {
    handleLogin(link, value);
  } else if (event === 'search') {
    handleSearch(link, value)
  } else if (event === 'call') {
    handleCall(link, value);
  } else if (event === 'accept') {
    handleAccept(link, value);
  } else if (event === 'disconnect') {
    handleDisconnect(link, value);
  } else if (event === 'refuse') {
    handleRefuse(link, value);
  } else if (event === 'ice' || event === 'offer' || event === 'answer') {
    handleDescription(link, value, event);
  }
}


// 处理登录
const handleLogin = (link, value) => {
  const user = users.get(value);
  if (user && user.state === 1) {
    link.send(transform({ event: "login", value: false, message: "重复登录" }));
  } else {
    users.set(value, { socket: link, state: 1 });
    link.send(transform({ event: "login", value: true, message: "登录成功" }));
  }
}


// 处理查找用户
const handleSearch = (link, value) => {
  const user = users.get(value);
  if (user && user.state === 1) {
    link.send(transform({ event: "search", value: true, message: `用户 ${value} 存在` }));
  } else {
    link.send(transform({ event: "search", value: false, message: `用户 ${value} 不存在` }));
  }
}

// 处理呼叫用户
const handleCall = (link, value) => {
  const target = users.get(value.target);
  const caller = value.caller;
  if (target.state === 1) {
    target.socket.send(transform({ event: "call", value: true, message: `${caller} 呼叫你`, person: caller }))
    link.send(transform({ message: `呼叫 ${value.target}` }))
  } else if (target.state === 2) {
    link.send(transform({ event: "refuse", value: false, message: `${value.target} 通话中` }));
  } else {
    link.send(transform({ event: "refuse", value: false, message: `呼叫 ${value.target} 失败` }));
  }
}


// 处理接受呼叫
const handleAccept = (link, value) => {
  const target = users.get(value.caller);
  const user = value.user;
  const caller = users.get(user);
  if (caller) {
    caller.state = 2;
  }
  if (target) {
    target.state = 2;
    target.socket.send(transform({ event: "accept", value: true, message: `${user} 接受呼叫` }))
    link.send(transform({ message: `已接受 ${value.caller} 呼叫` }))
  } else {
    link.send(transform({ event: "refuse", value: false, message: `呼叫者${value.caller} 已离线` }));
  }
}

// 处理挂断电话
const handleDisconnect = (link, value) => {
  const target = users.get(value.target);
  const user = users.get(value.user);
  if (target) {
    target.state = 1;
    target.socket.send(transform({ event: "disconnect", message: `${value.user} 已挂断` }));
  }
  if (user) {
    user.state = 1;
  }
  link.send(transform({ message: `停止呼叫 ${value.target}` }));
}


// 处理拒绝通话
const handleRefuse = (link, value) => {
  const target = users.get(value.target);
  if (target) {
    target.socket.send(transform({ event: "refuse", message: `${value.user} 拒绝通话` }));
  }
  link.send(transform({ message: `已拒绝 ${value.target} 的电话` }));
}

// 处理协商信息
const handleDescription = (link, data, event) => {
  const target = users.get(data.target);
  if (target) {
    target.socket.send(transform({ event: event, value: data.value, message: `${data.user} 发来 ${event}` }));
  }
  link.send(transform({ message: `向 ${data.target} 发送 ${event}` }));
}


const transform = (value) => JSON.stringify(value);